package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQueryParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional(isolation = Isolation.READ_COMMITTED,
                           rollbackFor = Exception.class,
                           propagation = Propagation.REQUIRED,timeout = 60)
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Transactional(readOnly = true)
    @Override
    public List<VehicleVO> selectVehicle(VehicleQueryParam vehicleQuery) {
        return vehicleMapper.selectVehicle(vehicleQuery);
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setStatus("1");
        vehicleMapper.insert(vehicle);
        //添加缓存
        //记录日志
        //鉴权
    }
    @Override
    public void updateVehicle(VehicleSaveParam vehicleSaveParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        vehicle.setUpdateTime(new Date());
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
        //添加缓存
        //记录日志
        //鉴权
    }

    @Override
    public void deleteVehicle(Long id) {
        vehicleMapper.deleteById(id);
    }

    @Override
    public void updateBindStatus(Long id, String bindStatus, Long geofenceId) {
        if("0".equals(bindStatus)){
            geofenceId=null;
        }
        vehicleMapper.updateBindStatus(id,bindStatus,geofenceId);
    }
}
